<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Category extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
		$this->load->library('tank_auth');
		//$this->lang->load('tank_auth');
        if(!$this->tank_auth->is_logged_in())
            redirect('administrator/auth');
        $this->load->model('categories','cat');
        $this->load->model('tank_auth/roles','rol');
    }

    /**
     * Default function
     */
    public function index($cat_type=false,$act = false){
        if(!$cat_type) show_404();
        $post= $this->input->post(null);
        $this->_layout['action'] = $this->uri->segment(5);
        $this->_layout['cat_id'] = $this->uri->segment(6);
        
        $categories = $this->cat->getCat();
        
        //$categories = $this->cat->getCat();
        $this->assign('categories',$categories);
        $this->assign('action','');
        $this->assign('cat_type',$cat_type);
        $this->_layout['cat_type'] = $cat_type;
       
        if($act)
        {   
            $this->_layout['roles']=$this->rol->get_roles();
            $this->_layout['categories'] = $categories;
            if($act=="save"){
                if($post['cat_name'] ==""){
                            _set_error_message('....');
                            redirect('administrator/category/'.$cat_type);
                        }
                if($post['cat_type']=='frontend')
                {
                    $post['link'] = base_url();
                    $post['cat_type'] = 'article';
                }
                switch ($post['action']) {
                    case 'create':                            
                        $catname = $this->_global->_serialize(array($this->session->userdata('lan')=>array('title'=>$post['cat_name'])));
                        $alias = $this->_global->_serialize(array($this->session->userdata('lan')=>array('alias'=>_set_alias($post['cat_name']))));
                        $desc = $this->_global->_serialize(array($this->session->userdata('lan')=>array('cat_desc'=>$post['cat_desc'])));
                        $is_menu=0;
                        if(isset($post['is_menu'])) $is_menu = $post['is_menu'];    
                        $is_feature=0;
                        if(isset($post['is_feature'])) $is_feature = $post['is_feature'];                 
                        $fields = array('parent_id'=>$post['parent_id']
                                    ,'cat_name'=>$catname
                                    ,'cat_alias'=>$alias
                                    ,'created_at'=>time()
                                    ,'access'=>$post['access']
                                    ,'is_menu'=>$is_menu
                                    ,'cat_desc'=>$desc
                                    ,'metakey'=>$post['metakey']
                                    ,'metadesc'=>$post['metadesc']
                                    ,'is_feature'=>$is_feature
                                    ,'cat_type'=>$post['cat_type']
                                    ,'link' =>$post['link']
                                    ,'ordering'=>$post['ordering']);
                        $rs = $this->cat->addCat($fields);
                        if($rs>0)
                            _set_success_message('Save successful');
                        else
                            _set_error_message('....');
                        unset($fields,$alias,$desc,$catname);
                        redirect('administrator/category/index/'.$cat_type);
                        break;
                    case 'edit':
                        $cat_name = $this->cat->getCat(array('cat_id'=>$post['cat_id']));
                        $category = $this->_global->_unserialize($cat_name->cat_name);
                        $alias = $this->_global->_unserialize($cat_name->cat_alias);    
                        $desc = $this->_global->_unserialize($cat_name->cat_desc);
                      
                        if($post['cat_type']=='frontend')
                        {
                            $post['link'] = base_url();
                        }
                        if(!isset($category[$this->session->userdata('lan')]))
                        {
                            $category[$this->session->userdata('lan')]['title']=$post['cat_name'];
                        }else
                            $category[$this->session->userdata('lan')]['title']=$post['cat_name'];
                        if(!isset($desc[$this->session->userdata('lan')]))
                        {
                            $desc[$this->session->userdata('lan')]['cat_desc']=$post['cat_desc'];
                        }else
                            $desc[$this->session->userdata('lan')]['cat_desc']=$post['cat_desc'];
                        if(!isset($alias[$this->session->userdata('lan')]))
                        {
                            $alias[$this->session->userdata('lan')]['alias']=_set_alias($post['cat_name']);
                        }else
                            $alias[$this->session->userdata('lan')]['alias']=_set_alias($post['cat_name']);
                        $is_menu=0;
                        if(isset($post['is_menu'])) $is_menu = $post['is_menu'];
                        $is_feature=0;
                        if(isset($post['is_feature'])) $is_feature = $post['is_feature'];
                        //var_dump($post['cat_desc']);exit();
                        $category = $this->_global->_serialize($category);
                        $desc = $this->_global->_serialize($desc);
                        $rs = $this->cat->update_category(array('parent_id'=>$post['parent_id']
                                                                ,'cat_name'=>$category
                                                                ,'cat_alias'=>$this->_global->_serialize($alias)
                                                                ,'updated_at'=>time()
                                                                ,'is_menu'=>$is_menu
                                                                ,'is_feature'=>$is_feature                                                                
                                                                ,'cat_desc'=>$desc
                                                                ,'access'=>$post['access']
                                                                ,'metakey'=>$post['metakey']
                                                                ,'link' =>$post['link']
                                                                ,'metadesc'=>$post['metadesc']
                                                                ,'cat_type'=>$post['cat_type'],'ordering'=>$post['ordering']),array('cat_id'=>$post['cat_id']));                        
                        
                        if($rs>0)
                            _set_success_message('Save successful');
                        else
                            _set_error_message('....');
                        unset($categories,$alias,$desc,$cat_name);
                        redirect('administrator/category/index/'.$cat_type);
                        break;
                }
                
            }elseif($act=="delete"){
                if(!is_numeric($this->_layout['cat_id']) || !$this->_layout['cat_id']){
                    _set_error_message('Cant delete this category');
                    redirect('administrator/category/index/'.$cat_type);
                }
                $rs = $this->cat->delete_category($this->_layout['cat_id']);
                if($rs>0){
                    _set_success_message('Deleted...');
                }else
                    _set_error_message('Cant delete this category');
                redirect('administrator/category/index/'.$cat_type);
            }elseif($act=="edit"){
                $this->_layout['category'] = $this->cat->getCat(array('cat_id'=>$this->_layout['cat_id']));
            }elseif($act=="create"){
                $this->_layout['cat_type'] = $cat_type;
            }
            else show_404();
            $form   = $this->load->view('layouts/form/category',$this->_layout,true);
            $this->assign('action',$form);
        }
    }

}